Jumpstarting the Arduino 101 by Yining Shi & Sagar Mohite
Author:Yining Shi & Sagar Mohite
Language: eng
Format: epub, mobi
ISBN: 9781680453867
Published: 2017-08-27T16:00:00+00:00
main.js
Let’s start off by defining some constants. serviceUuid and name will allow this central device (your browser) to connect to the correct service on your peripheral 101 device with these two identifiers.
const serviceUuid ="19b10000-e8f2-537e-4f6c-d104768a1214"; const name = 'LEDCB'; var ledCharacteristic;
Next, define the connectTo101() function, which will be called when you click the Connect button in the UI. In this function, you first create an options object, which defines filters to search for your BLE peripheral device. Then you use the Web Bluetooth API to requestDevice and find your peripheral device with given filters.
The navigator.bluetooth.requestDevice() function returns a JavaScript Promise object. A Promise is an object that is often used for asynchronous communications with JavaScript, such as talking to a server. Once you receive a response, the callback function in the Promise’s then handler is executed. If there was an error, the callback function in the catch handler is executed. You can learn more about Promises by checking out this excellent explanation by Google: https://developers.google.com/web/fundamentals/getting-started/primers/promises.
Next, you connect to this device and then get the service with a given UUID from this device. Once you get the service, you get its characteristics using service.getCharacteristics().
Now that you have the characteristics, you assign the first one in the characteristics array to your ledCharacteristic variable.
function connectTo101() { let options = { filters: [{ services: [serviceUuid], name: name }] } console.log('Requesting Bluetooth Device...'); navigator.bluetooth.requestDevice(options) .then(device => { console.log('Got device', device.name); return device.gatt.connect(); }) .then(server => { console.log('Getting Service...'); return server.getPrimaryService(serviceUuid); }) .then(service => { console.log('Getting Characteristics...'); return service.getCharacteristics(); }) .then(characteristics => { console.log('Got LED Characteristic'); ledCharacteristic = characteristics[0]; }) .catch(error => { console.log('Argh! ' + error); }); }
Lastly, you define your writeTo101() function. In this function, you first select the value of your input with an ID of input-box and get its value. Then you convert the value to a buffer array of 8-bit unsigned integers. You can then use the writeValue function from the Web Bluetooth API to write this buffer to the ledCharacteristic that was selected in the connectTo101() function.
You’ve now written a characteristic value to your peripheral 101 device. The Arduino sketch you wrote earlier will handle the rest.
function writeTo101() { let inputValue = document.getElementById('input-box').value; let bufferToSend = Uint8Array.of(inputValue); ledCharacteristic.writeValue(bufferToSend); console.log('Writing '+ inputValue + ' to led Characteristic...'); }
Download
Jumpstarting the Arduino 101 by Yining Shi & Sagar Mohite.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Whiskies Galore by Ian Buxton(40624)
Introduction to Aircraft Design (Cambridge Aerospace Series) by John P. Fielding(32367)
Small Unmanned Fixed-wing Aircraft Design by Andrew J. Keane Andras Sobester James P. Scanlan & András Sóbester & James P. Scanlan(32155)
Craft Beer for the Homebrewer by Michael Agnew(17473)
Turbulence by E. J. Noyes(7080)
The Complete Stick Figure Physics Tutorials by Allen Sarah(6658)
Kaplan MCAT General Chemistry Review by Kaplan(6083)
The Thirst by Nesbo Jo(5815)
Bad Blood by John Carreyrou(5798)
Learning SQL by Alan Beaulieu(5445)
Weapons of Math Destruction by Cathy O'Neil(5068)
Man-made Catastrophes and Risk Information Concealment by Dmitry Chernov & Didier Sornette(4776)
iGen by Jean M. Twenge(4714)
Digital Minimalism by Cal Newport;(4605)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(4534)
Audition by Ryu Murakami(4123)
Electronic Devices & Circuits by Jacob Millman & Christos C. Halkias(4061)
1,001 ASVAB Practice Questions For Dummies by Powers Rod(4056)
Pale Blue Dot by Carl Sagan(4031)